import { getMenuListApi, addMenuListApi, getUpdateMenuListApi, updateMenuListApi, deleteMenuListApi } from '@/api/menuManage'
import { getReportListApi } from '@/api/customReportManage'

const menuManage = {
  namespaced: true,
  state: {
    menuList: [],
    reportList : []
  },
  mutations: {
    SET_LIST: (state, list) => {
      state.menuList = list
    },
    SET_REPORT_LIST: (state, list) => {
      state.reportList = list
    }
  },
  actions: {
    /**
     * 获取所有报表
     * @param {*} param0 
     */
    getReportList ( { state, commit } ) { 
      return new Promise((resove, reject) => {
        getReportListApi()
          .then(res=>{
            if (res.code ==0) {
              commit('SET_REPORT_LIST', res.data.data);
              resove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 获取菜单列表
    getMenuList ( { state, commit } ) {
      return new Promise((resolve, reject) => {
        getMenuListApi()
          .then(res => {
            if (res.code==0) {
              commit('SET_LIST',res.data)
              resolve(res)
            }else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 新增菜单
    addMenuList ( { state, commit }, form ) {
      return new Promise((resolve, reject) => {
        addMenuListApi({ ...form })
          .then(res => {
            if (res.code==0) {
              resolve(res)
            }else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 编辑菜单
    updateMenuList ( { state, commit }, form ) {
      return new Promise((resolve, reject) => {
        updateMenuListApi({ ...form })
          .then(res => {
            if (res.code==0) {
              resolve(res)
            }else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 获取指定菜单
    getUpdateMenuList ( { state, commit }, id ) {
      return new Promise((resolve, reject) => {
        getUpdateMenuListApi({ id })
          .then(res => {
            if (res.code==0) {
              resolve(res)
            }else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 删除菜单
    deleteMenuList ( { state, commit }, id ) {
      return new Promise((resolve, reject) => {
        deleteMenuListApi({ id })
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    }
  },
  getters: {

  }
}

export default menuManage